﻿@model MyBlog2.ViewModels.ArticleViewModel
@{
    ViewData["Title"] = "添加文章";
    Layout = "~/Areas/Admin/Views/Shared/_AdminLayout.cshtml";
}

<h1>添加文章</h1>

<br />
<br />

<div class="row">
    <div class="col-md-12">
        <form id="blog-form" method="post">
            <input type="hidden" id="published" asp-for="Article.Published" />
            <input type="hidden" id="tagIds" asp-for="Article.TagIds" />

            <div class="form-group">
                <label class="control-label">分类名称</label>
                <select id="classify" class="form-control" asp-for="Article.ArticleClassifyId">
                    <option value="-1">请选择分类...</option>
                    @foreach (var a in Model.ArticleClassifies)
                    {
                        <option value="@a.Id">@a.Name</option>
                    }
                </select>

            </div>
            <div class="form-group">
                <label asp-for="Article.Flag" class="control-label"></label>
                <input id="flag" type="text" asp-for="Article.Flag" class="form-control" />

            </div>
            <div class="form-group">
                <label asp-for="Article.Title" class="control-label"></label>
                <input id="title" type="text" asp-for="Article.Title" class="form-control" />
            </div>
            <div class="form-group">
                <label asp-for="Article.Description" class="control-label"></label>
                <textarea id="description" asp-for="Article.Description" class="form-control" rows="5"></textarea>

            </div>
            <div class="form-group">
                <label asp-for="Article.Contents" class="control-label"></label>
                <div id="md-content" style="z-index: 1 !important;">
                    <textarea id="contents" asp-for="Article.Contents" class="form-control" rows="10"></textarea>
                </div>

            </div>
            @* 标签选择 *@
            <div class="form-group">
                <label for="tagSelect">选择标签</label>
                <select multiple class="form-control" id="tagSelect" style="width:50%;height:105px">
                    @foreach (var tag in Model.Tags)
                    {
                        <option value="@tag.Id">@tag.Name</option>
                    }
                </select>
                <button id="clear" type="button" class="btn btn-primary" style="margin-top:5px;">清除</button>
            </div>

            <div class="form-check">
                <div class="row">
                    <div class="col">
                        <input id="ShareStatement" type="checkbox" class="form-check-input" asp-for="Article.ShareStatement">
                        <label class="form-check-label" for="ShareStatement" asp-for="Article.ShareStatement"></label>
                    </div>
                    <div class="col">
                        <input id="CommentAbled" class="form-check-input" type="checkbox" asp-for="Article.CommentAbled">
                        <label class="form-check-label" for="CommentAbled" asp-for="Article.CommentAbled"></label>
                    </div>
                    <div class="col"></div>
                </div>
            </div>
            <br />
            <div class="form-group">
                <button id="save-btn" type="button" class="btn btn-primary">保存</button>
                <button id="publish-btn" type="button" class="btn btn-info">发布</button>
            </div>
        </form>
    </div>
</div>
<script>
    //清除多选
    $("#clear").click(function () {
        $("#tagSelect").val("");
    });

    // 初始化markdown编辑器----------------------
    var contentEditor;
    $(function () {
        contentEditor = editormd("md-content", {
            width: "100%",
            height: 640,
            syncScrolling: "single",
            path: "/lib/editormd/lib/"
        });
    });
    //-------------------------------------------

    //保存
    $("#save-btn").click(function () {
        //表单验证----------------------------------
        var classify = $("#classify").val();
        var flag = $("#flag").val();
        var title = $("#title").val();
        var description = $("#description").val();
        var contents = $("#contents").val();
        var tagids = $("#tagSelect").val();
        if (classify == "-1") {
            alert("请选择分类！");
            return;
        }
        if (flag == "") {
            alert("请输入标记，如原创/转载/翻译！");
            return;
        }
        if (title == "") {
            alert("请输入标题！");
            return;
        }
        if (description == "") {
            alert("请输入描述/简介！");
            return;
        }
        if (contents == "") {
            alert("请输入内容！");
            return;
        }
        if (tagids == "") {
            alert("请选择标签！");
            return;
        }
        //-------------------------------------------

        setSM();
        setTagIds();
        $.ajax({
            type: 'post',
            url: "/Admin/ArticleManage/AddArticlePost",
            data: $("#blog-form").serialize(),
            success: function (data) {
                window.location = "/Admin/ArticleManage/ArticleManage";
            },
        });
    });

    //发布
    $("#publish-btn").click(function () {
        //表单验证----------------------------------
        var classify = $("#classify").val();
        var flag = $("#flag").val();
        var title = $("#title").val();
        var description = $("#description").val();
        var contents = $("#contents").val();
        var tagids = $("#tagSelect").val();
        if (classify == "-1") {
            alert("请选择分类！");
            return;
        }
        if (flag == "") {
            alert("请输入标记，如原创/转载/翻译！");
            return;
        }
        if (title == "") {
            alert("请输入标题！");
            return;
        }
        if (description == "") {
            alert("请输入描述/简介！");
            return;
        }
        if (contents == "") {
            alert("请输入内容！");
            return;
        }
        if (tagids == "") {
            alert("请选择标签！");
            return;
        }
        //-------------------------------------------

        setPublished();
        setSM();
        setTagIds();
        $.ajax({
            type: 'post',
            url: "/Admin/ArticleManage/AddArticlePost",
            data: $("#blog-form").serialize(),
            success: function (data) {
                window.location = "/Admin/ArticleManage/ArticleManage";
            },
        });
    });

    //设置是否发布
    function setPublished() {
        $("#published").val(true);
    }
    //设置转载声明和评论是否开启
    function setSM() {
        if ($("#ShareStatement").is(":checked")) {
            $("#ShareStatement").val(true);
        } else {
            $("#ShareStatement").val(false);
        }
        if ($("#CommentAbled").is(":checked")) {
            $("#CommentAbled").val(true);
        } else {
            $("#CommentAbled").val(false);
        }
    }
    
    //设置tagids
    function setTagIds() {
        $("#tagIds").val(($("#tagSelect").val()));
    }
</script>